package com.neusoft.neuiotms.dcvm.task.cmdbSyn.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.neuiotms.dcvm.task.cmdbSyn.dto.CmdbSynSrcDataDTO;
import com.neusoft.neuiotms.dcvm.task.cmdbSyn.model.CmdbSynSrcData;
import com.neusoft.neuiotms.dcvm.task.cmdbSyn.respository.CmdbSynDataRepository;

@Service
public class CmdbSynSrcDataService {
    @Autowired
    private CmdbSynDataRepository cmdbSynSrcDataRepository;

    public List<CmdbSynSrcData> searchUpdate(final String assetType) {
        return this.cmdbSynSrcDataRepository.searchUpdate(assetType);
    }

    public List<CmdbSynSrcData> searchAdd(final String assetType) {
        return this.cmdbSynSrcDataRepository.searchAdd(assetType);
    }

    public List<CmdbSynSrcData> findByAssetTypeNotIn(final List<String> list) {
        return this.cmdbSynSrcDataRepository.findByAssetTypeNotIn(list);
    }

    public List<CmdbSynSrcData> findAll() {
        return this.cmdbSynSrcDataRepository.findAll();
    }

    @Transactional(rollbackFor = Exception.class)
    public CmdbSynSrcData saveAndFlush(final CmdbSynSrcData data) {
        return this.cmdbSynSrcDataRepository.save(data);
    }

    @Transactional(rollbackFor = Exception.class)
    public List<CmdbSynSrcData> saveAll(final List<CmdbSynSrcData> datas) {
        return this.cmdbSynSrcDataRepository.saveAll(datas);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteAllInBatch() {
        this.cmdbSynSrcDataRepository.deleteAllInBatch();
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(final List<CmdbSynSrcDataDTO> dtos) {
        dtos.forEach(dto -> {
            final CmdbSynSrcData data = this.cmdbSynSrcDataRepository.getOne(dto.getId());
            data.setUpdateTime(new Date());
            data.setSynCmdb(dto.getSynCmdb());
            data.setCmdbCode(dto.getCmdbCode());
            this.cmdbSynSrcDataRepository.save(data);

        });
    }

}
